package com.example.redissondemo.config;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;

/**
 * @author Liunh
 * @Date:
 * @Description:
 */
@Data
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {

    private final Lettuce lettuce = new Lettuce();
    @Value(value = "${spring.redis.database:0}")
    private int database = 0;
    private String url;
    @Value(value = "${spring.redis.host:localhost}")
    private String host;
    @Value(value = "${spring.redis.port:6379}")
    private int port;
    @Value(value = "${spring.redis.password:123456}")
    private String password;
    @Value(value = "${spring.redis.isEncryptPassword:false}")
    private boolean isEncryptPassword;
    //@Value(value = "${spring.redis.isCluster:false}")
    //private boolean isCluster;
    // 单机 SINGLE，主从模式 MASTERSLAVE，集群模式 CLUSTER，哨兵模式 SENTINEL
    @Value(value = "${spring.redis.model:SINGLE}")
    private String model;
    private boolean ssl;
    @Value(value = "${spring.redis.timeout:0}")
    private Duration timeout;
    private Sentinel sentinel;

    /**
     * Pool properties.（连接池的配置信息）
     */
    @Component
    @Data
    @ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
    public class Pool {

        @Value(value = "${spring.redis.lettuce.pool.max-active:-1}")
        private int maxActive;

        @Value(value = "${spring.redis.lettuce.pool.max-wait:-1}")
        private int maxWait;

        @Value(value = "${spring.redis.lettuce.pool.max-idle:20}")
        private int maxIdle;

        @Value(value = "${spring.redis.lettuce.pool.min-idle:6}")
        private int minIdle;

        @Value(value = "${spring.redis.lettuce.pool.soTimeout:60000}")
        private int soTimeout;

        @Value(value = "${spring.redis.lettuce.pool.time-between-eviction-runs:30000}")
        private int timeBetweenEvictionRuns;
        @Value(value = "${spring.redis.lettuce.pool.num-tests-per-eviction-run:10}")
        private int numTestsPerEvictionRun;
        @Value(value = "${spring.redis.lettuce.pool.min-evictable-idle-time:60000}")
        private int minEvictableIdleTime;
        //private Duration maxWait = Duration.ofMillis(-1);
    }

    /**
     * Cluster properties.（集群配置信息）
     */
    @Component
    @Data
    public class Cluster {
        @Value(value = "${spring.redis.cluster.nodes:127.0.0.1:6379}")
        private List<String> nodes;
        @Value(value = "${spring.redis.cluster.max-redirects:10}")
        private Integer maxRedirects;
    }

    /**
     * Redis sentinel properties.（哨兵属性信息）
     */
    //@Component
    //@ConfigurationProperties(prefix = "spring.redis.sentinel")
    public class Sentinel {

        private String master;
        private String nodes;
        private List<String> nodesList;

        public String getMaster() {
            return master;
        }

        public void setMaster(String master) {
            this.master = master;
        }

        public String getNodes() {
            return nodes;
        }

        public void setNodes(String nodes) {
            this.nodes = nodes;
        }

        public List<String> getNodesList() {
            this.nodesList = Arrays.asList(this.nodes.split(","));
            return nodesList;
        }

        public void setNodesList(List<String> nodesList) {
            this.nodesList = nodesList;
        }
    }

    /**
     * Lettuce client properties.
     */
    @Setter
    @Getter
    public class Lettuce {
        /**
         * Shutdown timeout.
         */
        private Duration shutdownTimeout = Duration.ofMillis(100);
        /**
         * Lettuce pool configuration.
         */
        private Pool pool;
    }
}
